﻿var templateFormListTableVue = new Vue({
    el: "#templateFormListTable",
    data: {
        pageModel: {
            pageIdx: 1,
            pageSize: 10,
            pageTotal: 0,
            pageLength: 0,
            reults: []
        },
        searchModel: {
            queryName: "",
            queryTypeID: -1,
            queryIsDisable: -1,
            orderByType: false,
            isAsc: true,
            isAll: true
        },
        typeDictionary: {},
        isDisableDictionary: {}
    },
    methods: {
        AddForm: function () {
            AddTemplateForm(this);
        },
        DisableFrom: function (formID, isDisable) {
            DisableTemplateForm(this, formID, isDisable);
        },
        EditForm: function (formID) {
            EditTemplateForm(this, formID);
        },
        DeleteForm: function (form) {
            DeleteTemplateForm(this, form);
        },
        GotoPage: function (toPageIdx) {
            this.pageModel.pageIdx = toPageIdx;
            ListTemplateFormForPage(this);
        },
        SearchForm: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.isAll = false;
            ListTemplateFormForPage(this);
        },
        SearchReset: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.queryName = "";
            this.searchModel.queryTypeID = -1;
            this.searchModel.queryIsDisable = -1;
            this.searchModel.isAll = true;
            ListTemplateFormForPage(this);
        }

    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.typeDictionary = GlobalDictionary.GetEquipmentTypeDictionary();
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();

        ListTemplateFormForPage(this);
    }
});

/**
 * 查询所有点检单模板信息
 */
function ListTemplateFormForPage(vue) {
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/RoutineInspection/ListTemplateFormForPage?v=" + Math.random(),
        data: { pageModel: vue.pageModel, searchModel: vue.searchModel },
        success: function (res) {
            if (res.status == "Success") {
                vue.pageModel = res.data;
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 新增点检单模板信息
 */
function AddTemplateForm(vue) {
    OpenAzModal("新增点检单模板Add a routine check doc template", "/RoutineInspection/Page/TemplateFormDetail?IsNew=true", function () {
        ListTemplateFormForPage(vue);
    });
}

/**
 * 更新点检单模板是否禁用
 */
function DisableTemplateForm(vue, formID, isDisable) {
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/RoutineInspection/DisableTemplateForm",
        data: { formID: formID, isDisable: isDisable },
        success: function (res) {
            if (res.status == "Success") {
                toastr.success("更新成功Update succeeded。");
                ListTemplateFormForPage(vue);
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("更新失败，请稍候再试Update failed. Please try again later。");
        }
    })
}

/**
 * 编辑点检单模板信息
 */
function EditTemplateForm(vue, formID) {
    OpenAzModal("编辑点检单模板Edit a routine check doc template", "/RoutineInspection/Page/TemplateFormDetail?FormID=" + formID, function () {
        ListTemplateFormForPage(vue);
    });
}

/**
 * 删除点检单模板信息
 */
function DeleteTemplateForm(vue, form) {
    var message = "确认删除点检单模板Confirm to delete the routine check doc template【" + form.Name + "】?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/RoutineInspection/DeleteTemplateForm",
            data: { FormID: form.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除点检单模板信息成功Routine check doc template information deleted successfully。");
                    ListTemplateFormForPage(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        })
    }
}